package compoundDB.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import compoundDB.core.ImportUserDialog;
import compoundDB.database.enumeration.DataType;

@SuppressWarnings("serial")
public class ImportUserDialogImpl implements ImportUserDialog
{
	private static final String[] dataTypes = new String[] { "String", "Long", "Integer", "Float", "Double", "Bytes" };
	private static final String nameForDialog = "User Import Clarification";
	String[] m_propertiesMap;
	String m_property;
	String m_selection;
	boolean newColumn;
	DataType newDataType;

	@Override
	public void display()
	{
		new DialogImp(m_propertiesMap);
	}

	@Override
	public String getPropertyMap()
	{
		return m_selection;
	}

	@Override
	public void setPropertiesMaps(String[] properties)
	{
		m_propertiesMap = properties.clone();
	}

	@Override
	public void setProperty(String property)
	{
		m_property = property;
	}

	@Override
	public DataType getNewColumnDatatype()
	{
		return newDataType;
	}

	@Override
	public boolean isNewcolumn()
	{
		return newColumn;
	}


	class DialogImp extends JDialog
	{

		private static final int dialogWidth = 500;
		private static final int dialogHieght = 200;
		private static final String createNewString = "Create New";

		private JTextFieldFileName newColumnName;
		private JComboBox dataType;
		private JPanel contentPane;
		private boolean newColEnabled;
		private JButton butConfirm;
		private DialogImp thisDialog;

		public DialogImp(String[] cols)
		{
			thisDialog = this;
			setUpDialog();

			JPanel radLabels = new JPanel();
			radLabels.setLayout(new BorderLayout(/* radLabels, BoxLayout.Y_AXIS */));
			radLabels.add(new JLabel("Please select the column that the property"),BorderLayout.NORTH);
			JPanel radLabels2 = new JPanel();
			radLabels2.add(new JLabel(m_property),BorderLayout.CENTER);
			radLabels.add(radLabels2);
			radLabels.add(new JLabel("     should go in or create a new coloumn"),BorderLayout.SOUTH);

			newColEnabled = true;
			contentPane = new JPanel();
			contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
			contentPane.add(Box.createVerticalStrut(10));
			contentPane.add(radLabels);
			contentPane.add(Box.createVerticalStrut(10));
			contentPane.add(createConstant(cols));
			contentPane.add(Box.createVerticalStrut(10));
			contentPane.add(createNewColumnPanel());
			contentPane.add(Box.createVerticalStrut(10));
			add(contentPane);
			this.pack();
			this.setVisible(true);
		}

		private Component createConstant(String[] cols)
		{
			String[] columns = new String[cols.length + 1];
			for (int i = 0; i < cols.length; i++)
			{
				columns[i + 1] = cols[i];
			}
			columns[0] = createNewString;

			final JComboBox toDo = new JComboBox(columns);
			toDo.addItemListener(new ItemListener()
			{
				@Override
				public void itemStateChanged(ItemEvent e)
				{

					if (e.getItem().toString().equals(createNewString))
					{
						newColEnabled = true;
						if (newColumnName != null)
						{
							newColumnName.setText("");
						}
					}
					else
					{
						newColEnabled = false;
					}

					refreshGui();
					butConfirm.setEnabled(!newColEnabled);
				}
			});

			butConfirm = new JButton("Confirm");
			butConfirm.setToolTipText("Confirm this is the column you want");
			butConfirm.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent e)
				{
					if (newColEnabled)
					{
						newColumn = true;
						m_selection = newColumnName.getText();
						String temp = dataType.getSelectedItem().toString();
						temp = temp.toUpperCase();
						newDataType = DataType.valueOf(temp);
					}
					else
					{
						newColumn = false;
						m_selection = toDo.getSelectedItem().toString();
					}
					thisDialog.dispose();
				}
			});
			butConfirm.setEnabled(false);
			JPanel big = new JPanel();
			big.add(Box.createHorizontalStrut(5));
			big.add(toDo);
			big.add(Box.createHorizontalStrut(5));
			big.add(butConfirm);
			big.add(Box.createHorizontalStrut(5));
			return big;
		}

		private void refreshGui()
		{
			newColumnName.setEnabled(newColEnabled);
			dataType.setEnabled(newColEnabled);
		}

		public void checkEmpty(DocumentEvent e)
		{
			if (e.getDocument().getLength() == 0)
			{
				butConfirm.setEnabled(false);
			}
			else
			{
				butConfirm.setEnabled(true);
			}
		}

		private Component createNewColumnPanel()
		{
			newColumnName = new JTextFieldFileName(30, true, false, true);
			newColumnName.setPreferredSize(new Dimension(120, 20));
			newColumnName.getDocument().addDocumentListener(new DocumentListener()
			{

				@Override
				public void changedUpdate(DocumentEvent e)
				{
					checkEmpty(e);
				}

				@Override
				public void insertUpdate(DocumentEvent e)
				{
					checkEmpty(e);
				}

				@Override
				public void removeUpdate(DocumentEvent e)
				{
					checkEmpty(e);
				}
			});

			dataType = new JComboBox(dataTypes);

			JPanel big = new JPanel();
			big.add(Box.createHorizontalStrut(5));
			big.add(newColumnName);
			big.add(Box.createHorizontalStrut(5));
			big.add(dataType);
			big.add(Box.createHorizontalStrut(5));
			return big;
		}

		private void setUpDialog()
		{
			this.setModal(true);
			setSize(dialogWidth, dialogHieght);
			setTitle(nameForDialog);
			setDefaultCloseOperation(DISPOSE_ON_CLOSE);

			Dimension size = getToolkit().getScreenSize();
			setLocation(size.width / 3 * 2 - getWidth() / 2, size.height / 3 - getHeight() / 2);

			this.setAlwaysOnTop(true);
		}
	}
}
